<?php 
include('./fun.php');
require './vendor/autoload.php';
include('./qrcode/qrlib.php');
$conn=my_sql($cfg);


use GuzzleHttp\Exception\RequestException;
use WechatPay\GuzzleMiddleware\WechatPayMiddleware;
use WechatPay\GuzzleMiddleware\Util\PemUtil;
use WechatPay\GuzzleMiddleware\Util\SensitiveInfoCrypto;
// 商户相关配置
$merchantId = '1603745452'; // 商户号
$merchantSerialNumber = '1CFBD2C91C53F974E9AC2264A00B12A7421C030F'; // 商户API证书序列号

$path=dirname(__FILE__);
$merchantPrivateKey = PemUtil::loadPrivateKey($path.'/key/x/apiclient_key.pem'); 
// 微信支付平台配置
$wechatpayCertificate = PemUtil::loadCertificate($path.'/key/x/key.pem'); 

// 构造一个WechatPayMiddleware
$wechatpayMiddleware = WechatPayMiddleware::builder()
    ->withMerchant($merchantId, $merchantSerialNumber, $merchantPrivateKey) // 传入商户相关配置
    ->withWechatPay([ $wechatpayCertificate ]) // 可传入多个微信支付平台证书，参数类型为array
    ->build();

// 将WechatPayMiddleware添加到Guzzle的HandlerStack中
$stack = GuzzleHttp\HandlerStack::create();
$stack->push($wechatpayMiddleware, 'wechatpay');

// 创建Guzzle HTTP Client时，将HandlerStack传入
$client = new GuzzleHttp\Client(['handler' => $stack]);




$dtl=['out_detail_no' =>'aa112a1aa1',
            'transfer_amount' => 1,
            'transfer_remark'=>'转账',
            'openid'=>'o9kl55s1B4VtgXvCF5CiZeaSAJ5A'];
try {
$resp = $client->request(
    'POST',
    'https://api.mch.weixin.qq.com/v3/transfer/batches', //请求URL
    [
        // JSON请求体
        'json' => [
            "appid" =>'wxa41314fcbdd717e0', 
            "out_batch_no" => "aaaa1a232a3", 
            "batch_name" => "转账1", 
            "batch_remark" =>"转账", 
            'total_amount'=>1,
            'total_num'=>1,
            "transfer_detail_list" => [$dtl]
        ],
        'headers' => [ 'Accept' => 'application/json',
        ]
    ]
);


$statusCode = $resp->getStatusCode();
if ($statusCode == 200) {



} else if ($statusCode == 204) { //处理成功，无返回Body
    echo "success";
}
} catch (RequestException $e) {
    // 进行错误处理
    //echo $e->getMessage()."\n";
    if ($e->hasResponse()) {
       //die('支付二维码产生错误，如有问题，请联系管理员');
      echo $e->getResponse()->getStatusCode().' '.$e->getResponse()->getReasonPhrase()."\n";
     
      
      
        $code=$e->getResponse()->getStatusCode();
        echo $code;
        if($code==200){
            
            
        }else{
        die('支付错误');
        }
    }
    return;
}

?>